package com.org.qrtrace.controller;


import com.org.qrtrace.pojo.Account;
import com.org.qrtrace.service.IAccountService;
import com.org.qrtrace.utils.JsonResult;
import com.org.qrtrace.utils.JwtUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bonnie
 * @since 2023-03-18
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理api")
public class AccountController {

    @Autowired
    private IAccountService service;

    @GetMapping("/lists")
    public JsonResult<?> getList() {
        return JsonResult.successInfo(service.list());
    }

    @GetMapping("/getById")
    public JsonResult<?> getById(String id){
        Account account = service.getById(id);
        if(account==null) {
            return JsonResult.fail("未查询到账号信息！");
        }
        return JsonResult.successInfo(account);
    }

    @PostMapping("/login")
    public JsonResult<?> login(Account user){
        Map<String,String> resUser = new HashMap<>();
        Map<String,String> res = new HashMap<>();
        log.info("user:"+user.getUser()+",password:"+user.getPassword());
        if(service.login(user)){
            resUser.put("user", user.getUser());
            resUser.put("password", user.getPassword());
            res.put("token", JwtUtils.createToken(resUser));
            return JsonResult.successInfo(res);
        }
        return JsonResult.fail("登陆失败，账号密码有误！");
    }

    @PostMapping("/signUp")
    public JsonResult<?> singUp(Account user){
        return null;
    }
}
